//
//  SearchMerchantParser2.m
//  FootPrint
//
//  Created by 秦 东方 on 5/27/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "SearchMerchantParser2.h"
#import "MerchantQueryRequest.pb.h"
#import "ASIDataProvider.h"

@implementation SearchMerchantParser2

- (NSString *)requestGroup
{
    return ASI_CANCEL_LAST_QUEUE;
}

- (void)requestMerchant:(long long)uid cityName:(NSString*)city districtName:(NSString*)district busiCircle:(NSString*)circle type:(NSString*)type subType:(NSString*)subType startIndex:(int)startIndex filter:(NSString*)filter{
    
    MerchantQueryRequestMessage_Builder* builder = [MerchantQueryRequestMessage builder];
    [builder setUserId:uid];
    
    if(nil != city && city.length > 0){
        [builder setCity:city];
    }
    
    if(nil != district && district.length > 0){
        [builder setDistrict:district];
    }
    
    if(nil != circle && circle.length > 0){
        [builder setBusinessCircle:circle];
    }
    
    if(nil != type && type.length > 0){
        [builder setType:type];
    }
    
    if(nil != subType && subType.length > 0){
        [builder setSubType:subType];
    }
    
    if(nil != filter && filter.length > 0){
        [builder setMerchantName:filter];
    }
    [builder setStartCur:startIndex];
    
    NSData* queryData = [[builder build] data];
    if(queryData){
        
        [super noCacheRequestURL:URL_MERCHANT postData:queryData completion:^(NSData* resultData){
            @try {
                MerchantQueryResponseMessage* resp = [MerchantQueryResponseMessage parseFromData:resultData];
                [_delegate didfinishReqMer2:resp];
            }
            @catch (NSException *exception) {
                [_delegate failReqMer2];
            }
            
        } fail:^(NSError * err) {
            [_delegate failReqMer2];
        }];
        
    } else {
        [_delegate failReqMer2];
    }
    
}

@end
